//
//  SYCommodityViewController.m
//  TermOfSectionB
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYCommodityViewController.h"
#import "SYImageTableViewCell.h"
#import "SYWebViewController.h"
#import "SYCommodityCollectionViewCell.h"
#import "UMSocial.h"
#import "SYBrandViewController.h"
#import "CLconter.h"
#import "SYTempModel.h"
#import "SYLabelTableViewCell.h"
#import "JRDataBaseManager.h"

#define SWIDTH [UIScreen mainScreen].bounds.size.width / 375.0
#define SHEIGHT [UIScreen mainScreen].bounds.size.height / 667.0

@interface SYCommodityViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property (nonatomic, strong) SYBaseTableView *myTableView;
// scroll三方
@property (nonatomic, strong) SDCycleScrollView *scroll;
@property(nonatomic, strong)UIView *smallView;
@property(nonatomic, strong)UIView *head;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UICollectionView *myCollectionView;
@property(nonatomic, retain)UIScrollView *myScrollView;
@property(nonatomic, retain)UIImageView *myImage;
@property(nonatomic, retain)NSString *ss;
@property(nonatomic, strong)SYTempModel *model;
@property(nonatomic, assign)CGFloat picSize;
@property(nonatomic, strong)UIView *headView;
@property(nonatomic, retain)UIPageControl *page;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UIScrollView *firstScrollView;
@property(nonatomic, retain)NSMutableArray *contArr;
@property(nonatomic, retain)NSString *usereId;
@property(nonatomic, retain)CLguidang *gui;
@property(nonatomic, retain)NSString *brandId;
@property(nonatomic, retain)NSString *productUrl;

@end

@implementation SYCommodityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor redColor];
    
    self.firstScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.firstScrollView];
    [self.firstScrollView makeConstraints:^(MASConstraintMaker *make) {
        
        WIDTHToView(self.view);
        HEIGHTToView(self.view);
    }];
    
    self.firstScrollView.backgroundColor = [UIColor grayColor];
    self.firstScrollView.contentSize = CGSizeMake(2 * 375 * SWIDTH, 0);
    self.firstScrollView.pagingEnabled = YES;
    self.firstScrollView.delegate = self;
    self.firstScrollView.showsHorizontalScrollIndicator = NO;
    
    ///////// 侧滑之后那页
    
    //    [CLsaveTool isMake];
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, -60 * SHEIGHT, 375 * SWIDTH, 60 * SHEIGHT)];
    
    self.myTableView = [[SYBaseTableView alloc] initWithFrame:CGRectMake(375 * SWIDTH, 0, 375 * SWIDTH, 603 * SHEIGHT) style:UITableViewStylePlain superView:self.view delegate:self rowHeight:1 separatorStyle:NO showsVerticalScrollIndicator:NO showsHorizontalScrollIndicator:NO header:self.headView];
    [self.firstScrollView addSubview:self.myTableView];

    self.headView.backgroundColor = [UIColor whiteColor];

    UILabel *headLabel = [[UILabel alloc] initWithFrame:CGRectMake(100 * SWIDTH, 10 * SHEIGHT, 170 * SWIDTH, 40 * SHEIGHT)];
    headLabel.text = @"商品描述";
    headLabel.font = [UIFont systemFontOfSize:18];
    headLabel.textColor = [UIColor grayColor];
    headLabel.textAlignment = NSTextAlignmentCenter;
    [self.headView addSubview:headLabel];

    self.model = [[SYTempModel alloc] init];
    
    ////////////// 第一页
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 603 * SHEIGHT)];
    self.myScrollView.backgroundColor = [UIColor grayColor];
        [self.firstScrollView addSubview:self.myScrollView];
//    [self.firstScrollView addSubview:self.myScrollView];
    self.myScrollView.showsVerticalScrollIndicator = NO;
    self.myScrollView.contentSize = CGSizeMake(0, 667 * 3 * SHEIGHT);
    //    self.myScrollView.bounces = NO;
  
    self.head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH * 375, 320)];
    [self.myScrollView addSubview:self.head];
    
    [self GZJcreateData];
    [self GZJcreateDete];
    [self GZJcreateMata];
    [self GZJcreateScrollView];
    
}

#pragma mark - ScrollView

- (void)GZJcreateScrollView
{
    self.scroll = [[SDCycleScrollView alloc] init];
    [self.head addSubview:self.scroll];
    self.scroll.placeholderImage = [UIImage imageNamed:@"nav_collected.png"];
    self.scroll.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.scroll.dotColor = [UIColor colorWithRed:0.6124 green:0.9486 blue:0.9881 alpha:1.0];
    [self.scroll makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(self.head);
        HEIGHTToNum(330);
        
    }];
}

- (void)reloadPicArr
{
    NSMutableArray *picArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.arr.count; i++) {
        [picArr addObject:self.arr[i]];
    }
    // 加入网络图片数组
    self.scroll.imageURLStringsGroup = picArr;
}

- (void)GZJcreateData {
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/product/content" POST:[NSDictionary dictionaryWithObjectsAndKeys:@"045c70806c52fe6326eb2a8562629d1d", @"key", [NSString stringWithFormat:@"%@", self.tempID], @"productId", @"1445944613582", @"t", @"xiangqu", @"type", @"1446082489042", @"t", @"705811", @"userId", @"705811", @"userid", @"3.5.4", @"version", nil] block:^(id result) {
        
        self.gui = [[CLguidang alloc] init];
        [self.gui setValuesForKeysWithDictionary:result[@"data"]];
        
        self.ss = result[@"data"][@"image"];
        // 价钱
        NSString *price = [NSString stringWithFormat:@"💰:%@", result[@"data"][@"price"]];
        // 包邮
        NSString *by = result[@"data"][@"postage"];
        // 标题
        
        self.model.title = result[@"data"][@"title"];
        
        self.model.temp = self.tempID;
        
//        NSLog(@"%@", self.title);
        // 喜欢
        NSString *like = [NSString stringWithFormat:@"%@", result[@"data"][@"favNum"]];
        // 主理人
        NSString *people = result[@"data"][@"productUser"];
        // 主理人头像
        NSString *avaPath = result[@"data"][@"avaPath"];
        // 品牌头像
        NSString *brandStory = result[@"data"][@"brandIcon"];
        // 品牌后
        NSString *brand = result[@"data"][@"brand"];
        // 厂家介绍
        NSString *brandSto = result[@"data"][@"brandStory"];
        //
        // 产品网址
        self.productUrl = result[@"data"][@"productURL"];
        // ID
        self.usereId = result[@"data"][@"userId"];
        
        self.brandId = result[@"data"][@"brandId"];
        self.arr = [NSMutableArray arrayWithArray:result[@"data"][@"imgsUrlList"]];
        [self.arr addObject:self.ss];
        
        [self reloadPicArr];
        
     //        ///////////////////
        self.smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 300 * SHEIGHT, 375 * SWIDTH, 100 * SHEIGHT)];
        self.smallView.backgroundColor = [UIColor whiteColor];
        [self.myScrollView addSubview:self.smallView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SWIDTH, 10 * SHEIGHT, 365 * SWIDTH, 50 * SHEIGHT)];
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.numberOfLines = 0;
        [_smallView addSubview:titleLabel];
        
        titleLabel.text = self.model.title;
        
        UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeSystem];
        buyButton.frame = CGRectMake(285 * SWIDTH, 45 *SHEIGHT, 80 *SWIDTH, 20 *SHEIGHT);
        [_smallView addSubview:buyButton];
        [buyButton setTitle:@"点我去购" forState:UIControlStateNormal];
        [buyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [buyButton addTarget:self action:@selector(cliBuy:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SWIDTH, 70 * SHEIGHT, 120 * SWIDTH, 20 * SHEIGHT)];
        [_smallView addSubview:priceLabel];
        
        priceLabel.textColor = [UIColor grayColor];
        priceLabel.text = price;
        
        UILabel *youLabel = [[UILabel alloc] initWithFrame:CGRectMake(110 * SWIDTH, 70 * SHEIGHT, 90 * SWIDTH, 20 * SHEIGHT)];
        youLabel.font = [UIFont systemFontOfSize:14];
        [_smallView addSubview:youLabel];
        
        youLabel.text = by;
        
        UIButton *thinkButton = [UIButton buttonWithType:UIButtonTypeSystem];
        thinkButton.frame = CGRectMake(320 * SWIDTH, 70 * SHEIGHT, 55 * SWIDTH, 20 * SHEIGHT);
        [_smallView addSubview:thinkButton];
        [thinkButton setTitle:@"分享" forState:UIControlStateNormal];
        [thinkButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [thinkButton addTarget:self action:@selector(fenxiang:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeSystem];
        likeButton.frame = CGRectMake(210 * SWIDTH, 70 * SHEIGHT, 50 * SWIDTH, 20 * SHEIGHT);
        [_smallView addSubview:likeButton];
        [likeButton setTitle:@"❤️:" forState:UIControlStateNormal];
        [likeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        UILabel *likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(260 * SWIDTH, 70 * SHEIGHT, 50 * SWIDTH, 20 * SHEIGHT)];
        [_smallView addSubview:likeLabel];
        
        likeLabel.text = like;
        /////////////// 下面框
        UIView *nextView = [[UIView alloc] initWithFrame:CGRectMake(0, 415 * SHEIGHT, 375 * SWIDTH, 50 * SHEIGHT)];
        nextView.backgroundColor = [UIColor whiteColor];
        [self.myScrollView addSubview:nextView];
        
        
        UIImageView *rrrImage = [[UIImageView alloc] initWithFrame:CGRectMake(5 * SWIDTH, 10 * SHEIGHT, 30 * SWIDTH, 30 * SHEIGHT)];
        [nextView addSubview:rrrImage];
        
        [rrrImage sd_setImageWithURL:[NSURL URLWithString:avaPath]];
        
        UILabel *leadLabel = [[UILabel alloc] initWithFrame:CGRectMake(45 * SWIDTH, 15 * SHEIGHT, 70 * SWIDTH, 20 * SHEIGHT)];
        leadLabel.textColor = [UIColor grayColor];
        leadLabel.text = @"主理人:";
        [nextView addSubview:leadLabel];
        
        
        UILabel *workLabel = [[UILabel alloc] initWithFrame:CGRectMake(118 * SWIDTH, 15 * SHEIGHT, 200 * SWIDTH, 20 * SHEIGHT)];
        [nextView addSubview:workLabel];
        
        workLabel.text = people;
        
        UIView *brandView = [[UIView alloc] init];
        brandView.backgroundColor = [UIColor whiteColor];
        [self.myScrollView addSubview:brandView];
        
        //
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taaaAction:)];
                [brandView addGestureRecognizer:tap];
        
        UIImageView *sssImage = [[UIImageView alloc] initWithFrame:CGRectMake(5 * SWIDTH, 10 * SHEIGHT, 30 * SWIDTH, 30 * SHEIGHT)];
        [brandView addSubview:sssImage];
        
        [sssImage sd_setImageWithURL:[NSURL URLWithString:brandStory]];
        
        UILabel *brandLabel = [[UILabel alloc] initWithFrame:CGRectMake(45 * SWIDTH, 15 * SHEIGHT, 70 * SHEIGHT, 20 * SWIDTH)];
        brandLabel.textColor = [UIColor grayColor];
        brandLabel.text = @"品牌:";
        [brandView addSubview:brandLabel];
        
        
        UILabel *woLabel = [[UILabel alloc] initWithFrame:CGRectMake(118 * SWIDTH, 15 * SHEIGHT, 200 * SWIDTH, 20 * SHEIGHT)];
        [brandView addSubview:woLabel];
        
        woLabel.text = brand;
        
        UILabel *proLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SWIDTH, 50 * SHEIGHT, 365 * SWIDTH, 40 * SHEIGHT)];
        proLabel.textColor = [UIColor grayColor];
        [brandView addSubview:proLabel];
        proLabel.numberOfLines = 0;
        proLabel.text = brandSto;
        [proLabel sizeToFit];
        brandView.frame = CGRectMake(0, 466 * SHEIGHT, 375 * SWIDTH, (proLabel.frame.size.height + 60) * SHEIGHT);
        /////////////////
                UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
                flowlayout.itemSize = CGSizeMake(50 * SWIDTH, 50 * SHEIGHT);
                self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, (527 + proLabel.frame.size.height) * SHEIGHT, 375 * SWIDTH, 90 * SHEIGHT) collectionViewLayout:flowlayout];
                self.myCollectionView.backgroundColor = [UIColor whiteColor];
                [self.myScrollView addSubview:self.myCollectionView];
        
                [self.myCollectionView registerClass:[SYCommodityCollectionViewCell class] forCellWithReuseIdentifier:@"raue"];
                flowlayout.sectionInset = UIEdgeInsetsMake(30 * SHEIGHT, 10 * SWIDTH, 2 * SHEIGHT, 2 * SHEIGHT);
                flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
                UILabel *liLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * SWIDTH, 2 * SHEIGHT, 100 * SWIDTH, 30 * SHEIGHT)];
                liLabel.text = [NSString stringWithFormat:@"喜欢:%@", like];
                liLabel.textColor = [UIColor grayColor];
                [self.myCollectionView addSubview:liLabel];
        
                self.myCollectionView.dataSource = self;
                self.myCollectionView.delegate = self;
        ////////////////////////
        
        UILabel *seeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (618 + proLabel.frame.size.height) * SHEIGHT, 375 * SWIDTH, 60 * SHEIGHT)];
        seeLabel.backgroundColor = [UIColor whiteColor];
        [self.myScrollView addSubview:seeLabel];
        seeLabel.text = @"左划为商品详情";
        seeLabel.textAlignment = NSTextAlignmentCenter;
        seeLabel.textColor = [UIColor grayColor];
        seeLabel.font = [UIFont systemFontOfSize:25];
        
        [self.myScrollView sizeToFit];
        self.myScrollView.contentSize = CGSizeMake(0, (680 + proLabel.frame.size.height) * SHEIGHT);
        
    }];

}

- (void)GZJcreateDete {
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/product/faver" POST:[NSDictionary dictionaryWithObjectsAndKeys:@"045c70806c52fe6326eb2a8562629d1d", @"key", [NSString stringWithFormat:@"%@", self.tempID], @"productId", @"1445944613582", @"t", @"xiangqu", @"type", @"705811", @"userId", @"705811", @"userid", @"3.5.4", @"version", nil] block:^(id result) {
        
        self.picArr = [NSMutableArray array];
        self.picArr = [CLchangePic baseModelByArr:result[@"data"]];
        [self.myCollectionView reloadData];
        
    }];
    
}

// 后面详情数据
- (void)GZJcreateMata {
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/product/chips" POST:[NSDictionary dictionaryWithObjectsAndKeys:@"045c70806c52fe6326eb2a8562629d1d", @"key", [NSString stringWithFormat:@"%@", self.tempID], @"productId", @"1445944613582", @"t", @"xiangqu", @"type", @"705811", @"userId", @"705811", @"userid", @"3.5.4", @"version", nil] block:^(id result) {
        
        self.contArr = [NSMutableArray array];
        self.contArr = [CLconter baseModelByArr:result[@"data"]];
        
        [self.myTableView reloadData];
        
    }];
}

- (void)tamAction:(UIGestureRecognizer *)tap11 {

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.picArr.count > 6) {
        return 6;
    } else {
        return self.picArr.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SYCommodityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"raue" forIndexPath:indexPath];
    CLchangePic *change = self.picArr[indexPath.row];
    if (self.picArr.count > 6) {
        if (indexPath.row == 5) {
            cell.likeImage.image = [UIImage imageNamed:@"right.png"];
        } else {
            [cell.likeImage sd_setImageWithURL:[NSURL URLWithString:change.avaPath]];
        }
        
    } else {
        [cell.likeImage sd_setImageWithURL:[NSURL URLWithString:change.avaPath]];
    }
    return cell;
    
}

- (void)taaaAction:(UITapGestureRecognizer *)tap {
    
    SYBrandViewController *artVC = [[SYBrandViewController alloc] init];
    [self.navigationController pushViewController:artVC animated:YES];
    // 传个id
    
    artVC.brandId = self.brandId;
    
}

//- (void)pageAction:(UIPageControl *)page {
//    [self.rigScrollView setContentOffset:CGPointMake((self.page.currentPage + 1)* 150, 0) animated:YES];
//    
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    if (scrollView == self.rigScrollView) {
//        if (scrollView.contentOffset.x == (self.arr.count + 1) * (self.view.frame.size.width)) {
//            scrollView.contentOffset = CGPointMake((self.view.frame.size.width), 0);
//        }
//        self.page.currentPage = scrollView.contentOffset.x / (self.view.frame.size.width);
//    }
//}

////////tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    CLconter *cont = self.contArr[indexPath.row];
    if ([cont.isImg integerValue] == 1) {
        self.myTableView.rowHeight = 300 * SHEIGHT;
        static NSString *reuse = @"reuu";
        SYImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYImageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        [cell.contImage sd_setImageWithURL:[NSURL URLWithString:cont.content]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        
        static NSString *reuse = @"rmmm";
        SYLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYLabelTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        //        self.myTabelView.rowHeight = cell.contlabel.frame.size.height + 50;
        cell.contlabel.text = cont.content;
        //    NSLog(@"%@", cont.content);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CLconter *cont = self.contArr[indexPath.row];

    if ([cont.isImg integerValue] == 0) {
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
        CGRect rect = [cont.content boundingRectWithSize:CGSizeMake(365 * SWIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return (rect.size.height + 5) * SHEIGHT;
        
    } else if([cont.isImg integerValue] == 1){
        
        UIImageView *image = [[UIImageView alloc] init];
        
        [image sd_setImageWithURL:[NSURL URLWithString:cont.content] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            if (image.size.width != 0) {
                
                self.picSize = image.size.height * (SWIDTH * [UIScreen mainScreen].bounds.size.width) / image.size.width;
            }
        }];
        
        return self.picSize;
        
    } else {
        
        return 100;
    }
}

//// 分享
- (void)fenxiang:(UIButton *)button {
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56389acb67e58ef61f00030c"
                                      shareText:[NSString stringWithFormat:@"分享:%@", self.productUrl]
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToRenren, UMShareToEmail,nil]
                                       delegate:nil];
}

- (void)cliBuy:(UIButton *)button {
    
    SYBaseWebView *webVC = [[SYBaseWebView alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    
    webVC.WebViewUrlStr = self.productUrl;
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
    [JRDataBaseManager createTableWithModelClass:[self.model class]];
    NSLog(@"%@", NSHomeDirectory());
    
    [JRDataBaseManager selectTableWithModelClass:[self.model class]];
    
    NSArray *arr = [JRDataBaseManager selectTableWithModelClass:[self.model class] withKey:@"temp"];
    
    NSLog(@"%@", self.tempID);
    
    for (NSInteger i = 0; i < arr.count; i++) {
        
        if (arr.count != 0  && [arr[i] isEqualToString:self.tempID]) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_collected.png"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
            self.navigationItem.rightBarButtonItem.tag = 1000;
        } else if (arr.count == 0) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_collection.png"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
            self.navigationItem.rightBarButtonItem.tag = 1001;
        }
    }
}

- (void)click:(UIBarButtonItem *)button {
    if (self.navigationItem.rightBarButtonItem.tag == 1001) {
        
        [JRDataBaseManager insertDataWithModel:self.model];
        
        [button setImage:[UIImage imageNamed:@"nav_collected.png"]];
        self.navigationItem.rightBarButtonItem.tag = 1000;
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:^{
            
        }];
        [ENAlert alertShowTitle:@"收藏成功" message:nil cancelButtonTitle:@"确定" otherButtonTitles:nil block:^(NSInteger buttonIndex) {
            
        }];
        
    } else {
        
        [JRDataBaseManager deleteTableWithModelClass:[self.model class] whereKey:@"temp" isValue:self.tempID];
        [button setImage:[UIImage imageNamed:@"nav_collection.png"]];
        self.navigationItem.rightBarButtonItem.tag = 1001;
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
            
        }];
        
        [alertView addAction:action];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
